﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.IO;

namespace danikla
{
    public partial class Customer : Form
    {
        public DataGridViewRow row;
        public string DataPath;
        public bool filesDoNotMatch = false;

        string name, phone;
        string[] ofiles;
        IList<string> files;
        IList<string> files_full;


        public Customer()
        {
            InitializeComponent();
        }

        private void Customer_Load(object sender, EventArgs e)
        {
            DataPath = dlgDanikla.DATADIR + dlgDanikla.DIR_DELIMITER + dlgDanikla.toDir(Text);
            dlgDanikla.mkDir(DataPath);

            name = row.Cells[0].Value.ToString();
            phone = row.Cells[1].Value.ToString();

            loadDocuments();
        }

        private void Customer_Shown(object sender, EventArgs e)
        {
            
        }

        private void loadDocuments()
        {
            //Enum files from disk
            ofiles = row.Cells[2].Value.ToString().Split(",".ToCharArray());
            string strOfiles = String.Join(",", ofiles);

            files = Directory.EnumerateFiles(DataPath).ToList();
            files_full = Directory.EnumerateFiles(DataPath).ToList();
            for (int i = 0; i < files.Count; i++)
            {
                files[i] = files[i].Replace(DataPath + dlgDanikla.DIR_DELIMITER, "");
            }
            string strFiles = String.Join(",", files);

            if (ofiles.Length != files.Count || strOfiles != strFiles)
            {
                filesDoNotMatch = true;
            }


            //Update the list
            lstDocuments.Clear();
            lstDocuments.Columns.Add("Naziv", 200);
            lstDocuments.Columns.Add("Datum", 80);

            ListViewItem li;
            FileInfo fi;
            string tmp;

            lstDocuments.SmallImageList = lstFileTypes;

            for (int i = 0; i < files.Count; i++)
            {
                fi = new FileInfo(files_full[i]);
                
                li = new ListViewItem();
                li.Text = files[i].ToString();

                tmp = files[i].Substring(files[i].LastIndexOf(".") + 1).ToLower();
                if(!lstFileTypes.Images.ContainsKey(tmp))
                {
                    tmp = "file";
                }
                li.ImageKey = tmp;
                li.SubItems.Add(fi.LastWriteTime.ToShortDateString());

                lstDocuments.Items.Add(li);
            }
        }

        private void menuDocAdd_Click(object sender, EventArgs e)
        {
            dlgOpenFile.InitialDirectory = ".";
            dlgOpenFile.FileName = "";
            dlgOpenFile.ShowDialog();
        }

        private void dlgOpenFile_FileOk(object sender, CancelEventArgs e)
        {
            OpenFileDialog o = (OpenFileDialog)sender;
            File.Copy(o.FileName, DataPath + dlgDanikla.DIR_DELIMITER + o.SafeFileName,true);
            loadDocuments();
        }

        private void lstDocuments_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            menuDocDelete.Enabled = true;
        }

        private void lstDocuments_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            menuDocDelete.Enabled = e.IsSelected;
        }

        private void menuDocDelete_Click(object sender, EventArgs e)
        {
            int numSelected = lstDocuments.SelectedItems.Count;
            if (numSelected > 0)
            {
                string toDelete = "";
                int i;
                for (i = 0; i < numSelected; i++)
                {
                    toDelete += "\n   " + (i+1) + ". " + lstDocuments.SelectedItems[i].Text;
                }
                if (MessageBox.Show("Da li ste sigurni da želite da obrišete odabrana dokumenta?" + toDelete, "Brisanje dokumenata", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    int index;
                    for (i = 0; i < numSelected; i++)
                    {
                        index = lstDocuments.SelectedItems[i].Index;
                        try
                        {
                            File.Delete(files_full[index]);
                        }
                        catch (Exception eDel)
                        {
                            MessageBox.Show(eDel.Message, "GREŠKA!");
                        }
                    }
                    loadDocuments();
                }
            }
        }

        private void Customer_FormClosing(object sender, FormClosingEventArgs e)
        {
            int index = row.Index;
            DataGridViewRow r = dlgDanikla.dgCustomers.Rows[index];

            string tmp="";
            for(int i=0; i<files.Count; i++)
            {
                tmp += ", " + files[i];
            }
            r.Cells[2].Value = Regex.Replace(tmp, @"^,\s*", "");

            dlgDanikla.saved = false;
            dlgDanikla.mSave.Enabled = true;
            dlgDanikla._editingInProgressTitle();
        }
    }
}
